<?php defined('BASEPATH') OR exit('No direct script access allowed');
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Member extends REST_Controller
{
	function user_get()
	{
		if(!$this->get('id'))
		{
			$this->response(NULL, 400);
		}
		$this->load->model('user_model');
		$listuser = $this->user_model->_listuser();
		$users = array();
		foreach($listuser as $row){
			$users[$row->id] = array('id'=>$row->id,
			'username'=>$row->username,
			'balance'=>$row->balance,
			'createdate'=>$row->createdate,
			'withdraw'=>$row->withdraw,
			'active'=>$row->active,
			'fullname'=>$row->fullname,
			'bankname'=>$row->bankname,
			'accbank'=>$row->accbank,
			'note'=>$row->note,
			'phone'=>$row->type,
			'type'=>$row->type,
			'email'=>$row->email,
			'usertype'=>$row->usertype); 
		}
		//var_dump($users);die();
		$user = @$users [$this->get('id')];
			
			
		if($user)
		{
			$this->response($user, 200); // 200 being the HTTP response code
		}

		else
		{
			$this->response(array('error' => 'User could not be found'), 404);
		}
	}


	function userall_get(){
			
		$this->load->model('user_model');
		$listuser = $this->user_model->_listuser();
		$users = array();
		foreach($listuser as $row){
			$users[$row->id] = array('id'=>$row->id,
			'username'=>$row->username,
			'balance'=>$row->balance,
			'createdate'=>$row->createdate,
			'withdraw'=>$row->withdraw,
			'active'=>$row->active,
			'fullname'=>$row->fullname,
			'bankname'=>$row->bankname,
			'accbank'=>$row->accbank,
			'note'=>$row->note,
			'phone'=>$row->type,
			'type'=>$row->type,
			'email'=>$row->email,
			'usertype'=>$row->usertype); 
		}
		//var_dump($users);die();
		//$user = @$users [$this->get('id')];

		$user = $listuser;

		if($user)
		{
			$this->response($user, 200); // 200 being the HTTP response code
		}

		else
		{
			$this->response(array('error' => 'User could not be found'), 404);
		}
	}
	function user_post()
	{
		//$this->some_model->updateUser( $this->get('id') );
		//$message = array('id' => $this->get('id'), 'name' => $this->post('name'), 'email' => $this->post('email'), 'message' => 'ADDED!');
		//$this->response($message, 200); // 200 being the HTTP response code
		$result = $this->user_model->update( $this->post('id'), array(
            'name' => $this->post('name'),  
            'email' => $this->post('email')  
		));

		if($result === FALSE)
		{
			$this->response(array('status' => 'failed'));
		}
		else
		{
			$this->response(array('status' => 'success'));
		}
	}

	function user_delete()
	{
		//$this->some_model->deletesomething( $this->get('id') );
		$message = array('id' => $this->get('id'), 'message' => 'DELETED!');

		$this->response($message, 200); // 200 being the HTTP response code
	}




	public function send_post()
	{
		var_dump($this->request->body);
	}


	public function send_put()
	{
		var_dump($this->put('foo'));
	}
}